package org.geometerplus.zlibrary.ui.android.view.animation;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;

import com.example.administrator.fangbook.read.bean.Chapter;
import com.example.administrator.fangbook.read.library.readSetting.ReadSettingUtil;
import com.example.administrator.fangbook.read.view.readView.ZLAndroidWidget;

import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.view.ZLViewEnums;
import org.geometerplus.zlibrary.text.model.ZLTextModel;

import java.util.HashMap;

/**
 * 页面、view和动画关联的管理类
 */
public class AnimationManager {

    /**
     * 用户锁对象
     */
    public static final byte[] mLockObj = new byte[0];
    private final ZLAndroidWidget mWidget;
    private final BitmapManager mBitmapManager;
    /**
     * 分线程
     */
    private HandlerThread mWorkingThread;
    private Handler mWorkingThreadHandler;

    /**
     * 动画缓存--到内存中
     */
    protected HashMap<ZLViewEnums.Animation, BaseAnimationProvider> mAnimations = new HashMap<ZLViewEnums.Animation, BaseAnimationProvider>();
    /**
     * 当前使用动画
     */
    private BaseAnimationProvider myCurAnimationProvider;

    /**
     * view使用 w、h
     */
    private int mWidth = -1, mHeight = -1;

    public AnimationManager(ZLAndroidWidget mReadView) {
        this.mWidget = mReadView;
        this.mWidget.setAnimationManager(this);
        this.mBitmapManager = new BitmapManager(mReadView);
    }

    /**
     * 设置阅读view的宽和高
     *
     * @param w
     * @param h
     */
    public void setSize(int w, int h) {
        if (mBitmapManager != null) {
            mWidth = w;
            mHeight = h;
            mBitmapManager.setSize(w, h);
        }
    }

    public void draw(Canvas canvas) {
        if (myCurAnimationProvider != null) {
            myCurAnimationProvider.onDraw(canvas);
        }
    }

    /**
     * 这里的作用：初始化线程，初始化当前使用的动画
     */
    public void start() {
        mWorkingThread = new HandlerThread("t_rendering_thread", Thread.NORM_PRIORITY);
        mWorkingThread.start();
        if (mWorkingThread.getLooper() != null) {
            mWorkingThreadHandler = new Handler(mWorkingThread.getLooper()) {
                @Override
                public void handleMessage(Message msg) {

                }
            };
        }
        //重置页面刷新动画--获取当前动画
        setAnimation(ReadSettingUtil.getFlipAnimation());
    }

    /**
     * 设置当前动画
     *
     * @param animationType
     */
    private void setAnimation(ZLViewEnums.Animation animationType) {
        myCurAnimationProvider = getAnimationProvider(animationType);
        if (myCurAnimationProvider != null) {
            myCurAnimationProvider.setSize(mWidth, mHeight);
            myCurAnimationProvider.resetAllBitmapCache();
            myCurAnimationProvider.close();
        }
    }

    /**
     * 根据动画类型得到对应动画
     *
     * @param animationType
     * @return
     */
    private BaseAnimationProvider getAnimationProvider(ZLViewEnums.Animation animationType) {
        BaseAnimationProvider animation = null;
        if (animationType == null) {
            return animation;
        }
        if (mAnimations != null && !mAnimations.isEmpty()) {
            animation = mAnimations.get(animationType);
        }
        if (animation == null) {
            switch (animationType) {
                case curl:
//                    animation = new CurlAnimationProvider(myWidget, this);
                    break;
                case shift:
//                    animation = new ShiftAnimationProvider(myWidget, this);
                    break;
                case slide:
//                    animation = new SlideAnimationProvider(myWidget, this);
                    break;
                case autoread_overlap:
//                    animation = new OverlapAnimationProvider(myWidget, this);
                    break;
                case autoread_scroll:
//                    animation = new ScrollAnimationProvider(myWidget, this);
                    break;
                case vertical:
//                    animation = new VerticalShiftAnimationProvider(myWidget, this);
                    break;
                case none:
                default:
                    animation = new NoneAnimationProvider(mWidget, this);
                    break;
            }
            if (animation != null) {
                mAnimations.put(animationType, animation);
            }
        }
        return animation;
    }

    public void setModel(ZLFile file, ZLTextModel model) {
        postRunnableToRenderThread(new Runnable() {
            @Override
            public void run() {
                //停止等待加载页面
//                mFBView.stopPendingLoadPageData();
//                mFBView.setModel(model, file.getStartPositioin());
                mWidget.repaint();
            }
        }, true);
    }

    public void postRunnableToRenderThread(Runnable r, boolean isFront) {
        if (mWorkingThreadHandler != null) {
            if (isFront) {
                //立即发送Mesg到队列，而且是放在队列最前面
                mWorkingThreadHandler.postAtFrontOfQueue(r);
            } else {
                mWorkingThreadHandler.post(r);
            }
        }
    }

    public void setPrevModelData(Chapter previousChapter, ZLTextModel model, boolean toFirstPage) {

    }

    public void setNextModelData(Chapter nextChapter, ZLTextModel model) {

    }

    public void loadFailed(ZLFile file, String errorMsg) {

    }

    /**
     * 得到缓存的bitmap
     *
     * @param index
     * @return
     */
    public Bitmap getBitmapCached(ZLViewEnums.PageIndex index) {
        return mBitmapManager != null ? mBitmapManager.getBitmapCached(index) : null;
    }

    /**
     * 准备当前页面信息
     */
    public void prepareCurrentPagePaintInfo() {
        if (mBitmapManager != null) {
            mBitmapManager.prepareCurrentPagePaintInfo();
        }
    }
}
